// 存储路由
const routerSave = (router) => {
  const list = JSON.parse(sessionStorage.getItem('_router')) || []
  if (list.length) {
    const flag = list.find((item) => item.name === router.name)
    if (!flag) {
      list.push(router)
      sessionStorage.setItem('_router', JSON.stringify(list))
    }
  } else {
    list.push(router)
    sessionStorage.setItem('_router', JSON.stringify(list))
  }
  return list
}

let path = ''

// 关闭路由
const routerClose = (route) => {
  const list = JSON.parse(sessionStorage.getItem('_router'))
  const index = list.findIndex((item) => item.name === route.name)
  const listRouter = list.filter((item) => item.name !== route.name)
  sessionStorage.setItem('_router', JSON.stringify(listRouter))
  if (index) {
    path = list[index - 1].path
  } else {
    if (listRouter.length) {
      path = list[1].path
    } else {
      path = '/'
    }
  }
  return listRouter
}

// 删除后的下一个路由
const routerNext = () => {
  return path
}

// 全部关闭
const AllClose = () => {
  sessionStorage.setItem('_router', JSON.stringify([]))
  path = '/'
  return []
}
export { routerSave, routerClose, AllClose, routerNext }
